package sim

import (
	"context"

	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/sagoo-cloud/sagooiot/api/v1/sim"
	"github.com/sagoo-cloud/sagooiot/internal/model"
	"github.com/sagoo-cloud/sagooiot/internal/service"
)

var SimFactory = cSimSim_factory{}

type cSimSim_factory struct{}

// GetSimFactoryList 获取列表
func (u *cSimSim_factory) GetSimFactoryList(ctx context.Context, req *sim.GetSimFactoryListReq) (res *sim.GetSimFactoryListRes, err error) {
	var reqData = new(model.GetSimFactoryListInput)
	gconv.Scan(req, &reqData)
	total, currentPage, dataList, err := service.SimSimFactory().GetSimFactoryList(ctx, reqData)
	res = new(sim.GetSimFactoryListRes)
	gconv.Scan(dataList, &res.Data)
	res.PaginationRes.Total = total
	res.PaginationRes.CurrentPage = currentPage
	return
}

// GetSimFactoryById 获取指定ID数据
func (u *cSimSim_factory) GetSimFactoryById(ctx context.Context, req *sim.GetSimFactoryByIdReq) (res *sim.GetSimFactoryByIdRes, err error) {
	data, err := service.SimSimFactory().GetSimFactoryById(ctx, req.Id)
	res = new(sim.GetSimFactoryByIdRes)
	gconv.Scan(data, &res)
	return
}

// AddSimFactory 添加数据
func (u *cSimSim_factory) AddSimFactory(ctx context.Context, req *sim.AddSimFactoryReq) (res *sim.AddSimFactoryRes, err error) {
	userInfo := service.Context().GetLoginUser(ctx)
	if userInfo == nil {
		err = gerror.New("未登录或TOKEN失效,请重新登录")
		return
	}
	var data = model.SimFactoryAddInput{}
	gconv.Scan(req, &data)
	// data.CreatedBy = userInfo.Id
	err = service.SimSimFactory().AddSimFactory(ctx, data)
	return
}

// EditSimFactory 修改数据
func (u *cSimSim_factory) EditSimFactory(ctx context.Context, req *sim.EditSimFactoryReq) (res *sim.EditSimFactoryRes, err error) {
	userInfo := service.Context().GetLoginUser(ctx)
	if userInfo == nil {
		err = gerror.New("未登录或TOKEN失效,请重新登录")
		return
	}
	var data = model.SimFactoryEditInput{}
	gconv.Scan(req, &data)
	// data.UpdateBy = userInfo.Id //如果需要保存信息，把这个打开
	err = service.SimSimFactory().EditSimFactory(ctx, data)
	return
}

// DeleteSimFactory 删除数据
func (u *cSimSim_factory) DeleteSimFactory(ctx context.Context, req *sim.DeleteSimFactoryReq) (res *sim.DeleteSimFactoryRes, err error) {
	if len(req.Ids) == 0 {
		err = gerror.New("ID参数错误")
	}
	err = service.SimSimFactory().DeleteSimFactory(ctx, req.Ids)
	return
}

// SetStatusSimFactory 设置厂商状态
func (u *cSimSim_factory) SetStatusSimFactory(ctx context.Context, req *sim.SetStatusSimFactoryReq) (res *sim.SetStatusSimFactoryRes, err error) {
	res = new(sim.SetStatusSimFactoryRes)
	err = service.SimSimFactory().SetStatusSimFactory(ctx, req.Id, req.Status)
	return
}
